using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace LANLauncher
{
	public class Settings
	{
		public List<Setting> settings = new List<Setting>();

		private bool SettingsNeedSaved;

		public Settings()
		{
		}

		public void AddSetting(string _name, string _value, Control _combobox, Label _label)
		{
			foreach (Setting setting in this.settings)
			{
				if (setting.Name != _name)
				{
					continue;
				}
				return;
			}
			this.settings.Add(new Setting(_name, _value, _combobox, _label));
		}

		public Setting GetSetting(string _id)
		{
			Setting setting;
			List<Setting>.Enumerator enumerator = this.settings.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Setting current = enumerator.Current;
					if (_id != current.Name)
					{
						continue;
					}
					setting = current;
					return setting;
				}
				return null;
			}
			finally
			{
				((IDisposable)enumerator).Dispose();
			}
			//return setting;
		}

		public void LoadFromFile()
		{
			string str = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "\\Rockstar Games\\L.A. Noire\\settings.ini");
			if (File.Exists(str))
			{
				StreamReader streamReader = new StreamReader(str);
				string end = streamReader.ReadToEnd();
				streamReader.Close();
				string[] strArrays = end.Split(new char[] { '\r' });
				for (int i = 0; i < (int)strArrays.Length; i++)
				{
					string str1 = strArrays[i];
					string[] strArrays1 = str1.Split(new char[] { ':' });
					strArrays1[0] = strArrays1[0].Trim();
					foreach (Setting setting in this.settings)
					{
						if (setting.Name != strArrays1[0])
						{
							continue;
						}
						if (strArrays1.GetLength(0) <= 1)
						{
							break;
						}
						string str2 = strArrays1[1].Trim();
						string str3 = str2;
						setting.Value = str2;
						setting.WorkingValue = str3;
						break;
					}
				}
			}
		}

		public void LocaliseSettings()
		{
			foreach (Setting setting in this.settings)
			{
				setting.LocaliseValues();
			}
		}

		public void SaveToFile()
		{
			if (!this.SettingsNeedSaved)
			{
				return;
			}
			foreach (Setting setting in this.settings)
			{
				setting.UpdateValue();
				if (setting.IsValid || setting.AttachedControl == null)
				{
					continue;
				}
				MessageBox.Show(StringLocaliser.LocaliseString("SETTINGNOTVALID", false));
				return;
			}
			string str = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "\\Rockstar Games\\L.A. Noire");
			if (!Directory.Exists(str))
			{
				Directory.CreateDirectory(str);
			}
			StreamWriter streamWriter = new StreamWriter(string.Concat(str, "\\settings.ini"));
			streamWriter.WriteLine("[LANSettings]");
			foreach (Setting setting1 in this.settings)
			{
				streamWriter.WriteLine(string.Concat(setting1.Name, ":", setting1.Value));
			}
			streamWriter.Close();
		}

		public void SetSettingsNeedSaved()
		{
			this.SettingsNeedSaved = true;
		}
	}
}